//
//  TLRemindLocalPush.m
//  KidReading
//
//  Created by telen on 16/4/12.
//  Copyright © 2016年 刘赞黄Telen. All rights reserved.
//

#import "TLRemindLocalPush.h"

@implementation TLRemindLocalPush

+ (NSArray *)getAllNoticeInQue
{
    return [[UIApplication sharedApplication] scheduledLocalNotifications];
}

+ (void)sendLocalNotification:(NSString *)message date:(NSDate*)pushDate repeatInterval:(NSCalendarUnit)repeat userInfo:(NSDictionary<NSString *, id <NSSecureCoding>>*) userInfo
{
    
    //设置什么时间推送通知
    NSDate *date = pushDate;
    
    //创建一个本地推送
    UILocalNotification *noti = [[UILocalNotification alloc] init];
    if (noti) {
        
        //设置推送时间
        noti.fireDate = date;
        //设置时区
        noti.timeZone = [NSTimeZone defaultTimeZone];
        //设置重复间隔 0 表示不重复
        noti.repeatInterval = repeat;
        
        //推送声音
        noti.soundName = UILocalNotificationDefaultSoundName;
        
        //内容
        noti.alertBody = message;
        
        //滑动按钮名称
        //noti.alertAction = action;
        
        //设置userinfo 方便在之后需要撤销的时候使用
        noti.userInfo = userInfo;
        
        
        UIApplication *app = [UIApplication sharedApplication];
        
        //显示在icon上的红色圈中的数子
        noti.applicationIconBadgeNumber++;
        
        //添加推送到uiapplication
        [app scheduleLocalNotification:noti];  //时间触发通知
        //[app presentLocalNotificationNow:noti];  //立即发送通知
    }
}

#pragma mark
#define TLLocalNotificationType @"TLLocalNotificationType"
#define TLLocalNotificationType_value @(10000)
+ (void)cleanLastRemind
{
    NSMutableArray* noticeArr = [NSMutableArray arrayWithArray:[self getAllNoticeInQue]];
    for (UILocalNotification* noti in noticeArr) {
        NSDictionary* noticeDic = noti.userInfo;
        if ([TLLocalNotificationType_value isEqualToNumber:noticeDic[TLLocalNotificationType]]) {
            [[UIApplication sharedApplication] cancelLocalNotification:noti];
        }
    }
}

+ (void)addRemindDelay:(NSInteger)day message:(NSString *)msg
{
    NSTimeInterval interval = 24*60*60*day;
    NSDate * date = [NSDate dateWithTimeInterval:interval sinceDate:[NSDate date]];
    [self sendLocalNotification:msg date:date repeatInterval:0 userInfo:@{TLLocalNotificationType:TLLocalNotificationType_value}];
}

@end
